/**
 * @description 标签页
 * @author MaoXiangDong
 * @version 2021/3/12
 * @Email: 1257538859@qq.com
 * @WeChat: 19908853414
 */
import setting from '@/setting'

const state = {
  // 是否显示
  tabsShow: setting.layout.tabsShow,
  // 已经打开的页面列表
  openViews: [],
  // 需要缓存的页面 name
  cachedViews: []
}

const mutations = {
  /**
   * @description 添加路由
   */
  add_open_view: (state, view) => {
    // 如果已经存在 就不添加 备注：后续可以考虑是否加入强制刷新功能
    if (!state.openViews.some(v => v.path === view.path)) {
      // 不存在 就添加
      state.openViews.push(
        // 合并对象
        Object.assign({}, view, {
          name: view.meta.name || '未命名'
        })
      )
    }
  },
  /**
   * @description 添加需要缓存的路由 自动筛选禁止缓存
   */
  add_cached_view: (state, view) => {
    // 如果不存在
    if (!state.cachedViews.includes(view.name)) {
      // 如果 路由设置开启缓存 就添加
      if (view.meta.cache) {
        state.cachedViews.push(view.name)
      }
    }
  },
  /**
   * @description 删除单个已打开的路由
   */
  del_open_view: (state, view) => {
    const index = state.openViews.findIndex(it => it.path === view.path)
    state.openViews.splice(index, 1)
  },
  /**
   * @description 删除单个已缓存的路由
   */
  del_cached_view: (state, view) => {
    const index = state.cachedViews.indexOf(view.name)
    if(index > -1){
      state.cachedViews.splice(index, 1)
    }
  },
  /**
   * @description 删除其他已打开的路由
   */
  del_others_open_views: (state, view) => {
    // 固定tabs的不删除
    state.openViews = state.openViews.filter(v => {
      return v.meta.affix || v.path === view.path
    })
  },
  /**
   * @description 删除其他已缓存的路由
   */
  del_others_cached_views: (state, view) => {
    const index = state.cachedViews.indexOf(view.name)
    if (index > -1) {
      state.cachedViews = state.cachedViews.slice(index, index + 1)
    } else {
      state.cachedViews = []
    }
  },
  /**
   * @description 删除所有已打开的路由
   */
  del_all_open_views: state => {
    // 只保留 affix标签
    state.openViews = state.openViews.filter(tag => tag.meta.affix)
  },
  /**
   * @description 删除所有已缓存的路由
   */
  del_all_cached_views: state => {
    state.cachedViews = []
  },
  /**
   * @description 更新已打开的路由
   */
  update_open_view: (state, view) => {
    for (let v of state.openViews) {
      if (v.path === view.path) {
        v = Object.assign(v, view)
        break
      }
    }
  }
}

const actions = {
  /**
   * @description 同时添加已打开的路由并缓存
   */
  addView({ dispatch }, view) {
    dispatch('addOpenView', view)
    dispatch('addCachedView', view)
  },
  /**
   * @description 添加单个路由不缓存
   */
  addOpenView({ commit }, view) {
    commit('add_open_view', view)
  },
  /**
   * @description 添加单个需要缓存缓存路由
   */
  addCachedView({ commit }, view) {
    commit('add_cached_view', view)
  },
  /**
   * @description 同时删除已打开和已缓存的路由
   */
  delView({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delOpenView', view)
      dispatch('delCachedView', view)
      resolve({
        openViews: [...state.openViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  /**
   * @description 删除已打开的路由
   */
  delOpenView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('del_open_view', view)
      resolve([...state.openViews])
    })
  },
  /**
   * @description 删除已缓存的路由
   */
  delCachedView({ commit, state }, view) {
    return new Promise(resolve => {
      commit('del_cached_view', view)
      resolve([...state.cachedViews])
    })
  },
  /**
   * @description 同时删除其他已打开和已缓存的路由
   */
  delOthersViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delOthersOpenViews', view)
      dispatch('delOthersCachedViews', view)
      resolve({
        openViews: [...state.openViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  /**
   * @description 删除其他已打开的路由
   */
  delOthersOpenViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('del_others_open_views', view)
      resolve([...state.openViews])
    })
  },
  /**
   * @description 删除其他已缓存的路由
   */
  delOthersCachedViews({ commit, state }, view) {
    return new Promise(resolve => {
      commit('del_others_cached_views', view)
      resolve([...state.cachedViews])
    })
  },
  /**
   * @description 同时删除所有已打开和已缓存的路由
   */
  delAllViews({ dispatch, state }, view) {
    return new Promise(resolve => {
      dispatch('delAllOpenViews', view)
      dispatch('delAllCachedViews', view)
      resolve({
        openViews: [...state.openViews],
        cachedViews: [...state.cachedViews]
      })
    })
  },
  /**
   * @description 删除所有已打开的路由
   */
  delAllOpenViews({ commit, state }) {
    return new Promise(resolve => {
      commit('del_all_open_views')
      resolve([...state.openViews])
    })
  },
  /**
   * @description 删除所有已缓存的路由
   */
  delAllCachedViews({ commit, state }) {
    return new Promise(resolve => {
      commit('del_all_cached_views')
      resolve([...state.cachedViews])
    })
  },
  /**
   * @description 更新单个已打开的路由
   */
  updateOpenView({ commit }, view) {
    commit('update_open_view', view)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
